package com.freak.freakmusic.base

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.freak.freakmusic.app.App
import com.freak.freakmusic.app.App.Companion.DESIGN_WIDTH


/**
 * 无MVP的activity基类
 *
 * @author freak
 * @date 2019/9/11.
 */

abstract class BaseAbstractSimpleActivity : BaseActivity() {

    protected lateinit var mActivity: AppCompatActivity

    /**
     * 绑定布局
     *
     * @return
     */
    protected abstract val layout: Int

    /**
     * 初始化事件和数据
     */
    protected abstract fun initEventAndData()

    /**
     * 资源释放
     */
    protected abstract fun onDestroyRelease()

    /**
     * 控件初始化
     */
    protected abstract fun initView()

    override fun onCreate(savedInstanceState: Bundle?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        App.resetDensity(this, DESIGN_WIDTH.toFloat())
        setContentView(layout)
        super.onCreate(savedInstanceState)
        //绑定初始化ButterKnife
//        ButterKnife.bind(this)
        mActivity = this
        initView()
        initEventAndData()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        onDestroyRelease()
    }

    /**
     * 打开一个Activity 默认 不关闭当前activity
     *
     * @param className
     */
    fun gotoActivity(className: Class<*>) {
        gotoActivity(className, false)
    }

    /**
     * 打开一个Activity  关闭当前activity
     *
     * @param className
     */
    fun gotoActivityWithFinish(className: Class<*>) {
        gotoActivity(className, true)
    }

    /**
     * 打开一个Activity，并控制是否finish
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     */
    fun gotoActivity(className: Class<*>, isCloseCurrentActivity: Boolean) {
        gotoActivity(className, isCloseCurrentActivity, null)
    }
    /**
     * 打开一个activity，并传递数据
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     * @param bundle                 bundle数据
     */
    fun gotoActivity(
        className: Class<*>,
        isCloseCurrentActivity: Boolean = false,
        bundle: Bundle? = null
    ) {
        val intent = Intent(this, className)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
        if (isCloseCurrentActivity) {
            finish()
        }
    }

    /**
     * 打开一个带结果返回的activity，并传递数据
     *
     * @param className              className
     * @param bundle                 bundle数据
     * @param requestCode            请求码
     */
    fun gotoActivityWithResult(className: Class<*>, bundle: Bundle?, requestCode: Int) {
        val intent = Intent(this, className)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }


    /**
     * 系统返回键
     *
     * @param item
     * @return
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                //返回
                super.onBackPressed()
                return true
            }
            else -> return super.onOptionsItemSelected(item)
        }
    }

}
